<script setup>
import { ref, computed, onMounted } from "vue";

// 内置提供商列表
const builtInProviders = [
  { id: "volcengine", name: "火山引擎" },
  { id: "alibaba", name: "阿里巴巴百炼" },
  { id: "tencent", name: "腾讯混元" },
  { id: "xfyun", name: "科大讯飞" },
];

// 存储自定义提供商
const customProviders = ref([]);

// 合并内置和自定义提供商
const allProviders = computed(() => {
  return [...builtInProviders, ...customProviders.value];
});

const provider = ref("");
const providerName = ref("");
const isConfigured = ref(false);

onMounted(async () => {
  try {
    // 获取所有设置
    const allOptions = await browser.storage.sync.get(null);

    // 加载自定义提供商
    if (allOptions.customProviders && Array.isArray(allOptions.customProviders)) {
      customProviders.value = allOptions.customProviders;
    }

    // 检查提供商是否设置
    if (allOptions.provider) {
      provider.value = allOptions.provider;
      
      // 根据provider的id查找对应的name
      const selectedProvider = allProviders.value.find(p => p.id === provider.value);
      providerName.value = selectedProvider ? selectedProvider.name : provider.value;
      
      // 如果有提供商，就认为已配置
      isConfigured.value = true;
    } else {
      // 未设置提供商，显示未配置状态
      isConfigured.value = false;
    }
  } catch (error) {
    console.error("加载设置出错:", error);
  }
});

const openOptions = () => {
  browser.runtime.openOptionsPage();
};
</script>

<template>
  <div class="container">
    <header class="header">
      <h1>AI机务问答助手</h1>
      <div class="logo">AI</div>
    </header>

    <main class="content">
      <div v-if="isConfigured" class="info-section">
        <div class="status">
          <div class="status-icon active"></div>
          <div class="status-text">
            <h3>已连接到AI服务</h3>
            <p>当前提供商: <strong>{{ providerName }}</strong></p>
          </div>
        </div>
        <div class="info-message">
          <p>请在机务在线网站上使用右侧悬浮按钮进行试题捕获和问答</p>
        </div>
      </div>

      <div v-else class="warning-section">
        <div class="status">
          <div class="status-icon inactive"></div>
          <div class="status-text">
            <h3>未连接到AI服务</h3>
            <p>您需要先配置AI服务提供商</p>
          </div>
        </div>
        <div class="warning-message">
          <p>完成设置后，可在机务在线网站上使用悬浮按钮进行试题捕获和问答</p>
        </div>
      </div>
    </main>

    <footer class="footer">
      <button class="settings-btn" @click="openOptions">
        <span class="icon">⚙️</span>
        <span>设置</span>
      </button>
    </footer>
  </div>
</template>

<style scoped>
.container {
  width: 360px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 300px;
  background-color: #f8f9fa;
  border-radius: 8px;
  overflow: hidden;
}

.header {
  background-color: #4f9bf8;
  color: white;
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
}

.header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.logo {
  width: 36px;
  height: 36px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 16px;
}

.content {
  flex: 1;
  padding: 20px;
}

.status {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.status-icon {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 12px;
}

.status-icon.active {
  background-color: #48bb78;
  box-shadow: 0 0 0 4px rgba(72, 187, 120, 0.2);
}

.status-icon.inactive {
  background-color: #e53e3e;
  box-shadow: 0 0 0 4px rgba(229, 62, 62, 0.2);
}

.status-text h3 {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
}

.status-text p {
  margin: 0;
  font-size: 14px;
  color: #4a5568;
}

.info-section,
.warning-section {
  background-color: white;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.info-message,
.warning-message {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #edf2f7;
}

.info-message p,
.warning-message p {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  color: #4a5568;
}

.footer {
  padding: 16px 20px;
  border-top: 1px solid #edf2f7;
  display: flex;
  justify-content: center;
}

.settings-btn {
  display: flex;
  align-items: center;
  padding: 8px 20px;
  background-color: #4f9bf8;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.1s;
}

.settings-btn:hover {
  background-color: #3a8be0;
}

.settings-btn:active {
  transform: scale(0.98);
}

.settings-btn .icon {
  margin-right: 8px;
  font-size: 16px;
}
</style>
